home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / general / Dictionary.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  3.2 KB  |  122 lines

  1. " ------------------------------------------------------------- "
  2. " Dictionaries are implemented using Points in order to reduce  "
  3. " the number of classes in the standard prelude.  This also has "
  4. " the advantage of making the output appear in                  "
  5. "     'key @ value'    form                                     "
  6. " ------------------------------------------------------------- "
  7.  
  8. Class Dictionary :KeyedCollection
  9. ! hashTable currentBucket currentList !
  10. [
  11.    new
  12.  
  13.       hashTable <- Array new: 17
  14. |
  15.    hashNumber: aKey
  16.  
  17.       ^ (<primitive 5 aKey> intNegRem: hashTable size) + 1
  18. |
  19.    getList: aKey      ! list bucketNumber !
  20.  
  21.       bucketNumber <- self hashNumber: aKey.
  22.       list         <- hashTable at: bucketNumber.
  23.  
  24.       (list isNil)
  25.          ifTrue: [ list <- List new.
  26.                    hashTable at: bucketNumber put: list ].
  27.       ^ list
  28. |
  29.    at: aKey put: anObject   ! list anAssoc !
  30.       
  31.       amigatalk tracingOff.
  32.       
  33.       list    <- self getList: aKey.
  34.       anAssoc <- self findAssociation: aKey inList: list.
  35.  
  36.       (anAssoc isNil)
  37.          ifTrue:  [ anAssoc <- (Point new x: aKey) y: anObject.
  38.                     list add: anAssoc   ]
  39.          ifFalse: [ anAssoc y: anObject ].
  40.  
  41.       amigatalk tracingOn.
  42.       
  43.       ^ anObject
  44. |
  45.    at: aKey ifAbsent: exceptionBlock   ! list anAssoc ! 
  46.  
  47.       amigatalk tracingOff.
  48.       
  49.       list    <- self getList: aKey.
  50.       anAssoc <- self findAssociation: aKey inList: list.
  51.  
  52.       (anAssoc isNil)
  53.          ifTrue: [^ exceptionBlock value].
  54.  
  55.       amigatalk tracingOn.
  56.       
  57.       ^ anAssoc y
  58. |
  59.    removeKey: aKey ifAbsent: exceptionBlock    ! list anAssoc !
  60.  
  61.       list    <- self getList: aKey.
  62.       anAssoc <- self findAssociation: aKey inList: list.
  63.  
  64.       (anAssoc isNil)
  65.          ifTrue: [^ exceptionBlock value].
  66.  
  67.       ^ ( list remove: anAssoc 
  68.           ifAbsent: [ ^ exceptionBlock value ] ) y
  69. |
  70.    findAssociation: aKey inList: linkedList
  71.  
  72.       amigatalk tracingOff.
  73.       
  74.       linkedList do: [:item | (item x = aKey) 
  75.                                 ifTrue: [^ item] ].
  76.  
  77.       ^ nil
  78. |
  79.    first ! item !
  80.  
  81.       (1 to: 17) 
  82.          do: [ :i | ((item <- self checkBucket: i) notNil)
  83.                       ifTrue: [ ^ item y ]                 ].
  84.       ^ nil
  85. |
  86.    next         ! item !
  87.  
  88.       amigatalk tracingOff.
  89.       
  90.       ((item <- currentList next) notNil)
  91.          ifTrue: [ ^ item y ].
  92.  
  93.       [currentBucket < 17] 
  94.          whileTrue: [ currentBucket <- currentBucket + 1.
  95.                       ((item <- self checkBucket: currentBucket) notNil)
  96.                           ifTrue: [ ^ item y ] ].
  97.       ^ nil
  98. |
  99.    printString
  100.    
  101.       ^ (self inject: (self class printString) , ' ( '
  102.                 into: [ :aString :aValue |
  103.                         aString , self currentKey printString ,
  104.                         ' @ ' , aValue printString , ' ' ]
  105.                 ) , ')'
  106. |
  107.    currentKey   ! clist !
  108.    
  109.       ^ (currentList notNil) 
  110.          ifTrue: [ clist <- currentList current.
  111.  
  112.                    (clist notNil) 
  113.                      ifTrue: [clist x] ]
  114. |
  115.    checkBucket: bucketNumber
  116.  
  117.      ((currentList <- hashTable at: (currentBucket <- bucketNumber)) isNil)
  118.         ifTrue: [ ^ nil ].
  119.  
  120.      ^ currentList first
  121. ]
  122.